package com.dc.aimc.user.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dc.aimc.common.service.response.JsonResponse;
import com.dc.aimc.user.dto.UserDTO;
import com.dc.aimc.user.entity.User;
import com.dc.aimc.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 冯照龙
 * @since 2021-12-10
 */
@Log4j2
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户信息
     * @param user
     * @param request
     * @return
     */
    @ApiOperation(value = "添加用户信息")
    @PostMapping("/user")
    public JsonResponse<User> addUser(@RequestBody User user, HttpServletRequest request) {
        String tenant = request.getHeader("tenant-id");
        user.setTenantId(tenant);
        String salt = UUID.randomUUID().toString().substring(0, 4);
        user.setSalt(salt);
        String password = SecureUtil.sha1(user.getPassword() + salt);
        user.setPassword(password);
        userService.save(user);
        return JsonResponse.success();
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @ApiOperation(value = "更新用户信息")
    @PutMapping("/user")
    public JsonResponse<User> updateUser(@RequestBody User user) {
        if (StringUtils.isNotBlank(user.getPassword())) {
            String salt = UUID.randomUUID().toString().substring(0, 4);
            user.setSalt(salt);
            String password = SecureUtil.sha1(user.getPassword() + salt);
            user.setPassword(password);
        }
        userService.updateById(user);
        return JsonResponse.success();
    }

    /**
     * 根据username获取用户信息
     * @param username
     * @return
     */
    @ApiOperation(value = "根据用户username查询用户信息")
    @GetMapping("/user/{username}")
    public JsonResponse<User> getUserByUsername(@PathVariable("username") String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = userService.getOne(wrapper);
        return JsonResponse.success(user);
    }

    /**
     * 根据ID删除用户信息
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据用户ids查询用户信息")
    @DeleteMapping("/user/{ids}")
    public JsonResponse<User> deleteUser(@PathVariable("ids") Integer... ids) {
        userService.removeByIds(Arrays.asList(ids));
        return JsonResponse.success();
    }


    /**
     * 根据dto参数对象获取用户信息
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据用户dto参数查询用户信息")
    @PostMapping("/user/dto")
    public JsonResponse<Object> getUser(@RequestBody UserDTO dto) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, dto.getUsername());
        if (dto.getStatus() != null) {
            wrapper.eq(User::getStatus, dto.getStatus());
        }
        if (dto.getIsDeleted() != null) {
            wrapper.eq(User::getIsDeleted, dto.getIsDeleted());
        }
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            IPage<User> page = userService.page(new Page<>(dto.getPageNum(), dto.getPageSize()), wrapper);
            return JsonResponse.success(page);
        }
        List<User> apps = userService.list(wrapper);
        return JsonResponse.success(apps);
    }
}

